/**
 * Copyright (c) 2009-2016 https://github.com/zhanglongjian
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.ggy.utils;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.apache.log4j.Logger;

/**
 * <p>Log4j日志工具类</p>
 * <p> @createBy zhang longjian</p>
 * <p> @createTime 2017年7月27日 上午12:28:04 </p>
 * <p>TODO</p>
 */
public class Log4jUtil {
	public static Logger log = Logger.getLogger(Log4jUtil.class);

	/**
	 * 打印警告
	 * 
	 * @param obj
	 */
	public static void warn(Object obj) {
		try {
			/*** 获取输出信息的代码的位置 ***/
			String location = "";
			StackTraceElement[] stacks = Thread.currentThread().getStackTrace();
			location = stacks[2].getClassName() + "."
			        + stacks[2].getMethodName() + "("
			        + stacks[2].getLineNumber() + ")";
			/*** 是否是异常  ***/
			if (obj instanceof Exception) {
				Exception e = (Exception) obj;
				StringWriter sw = new StringWriter();
				e.printStackTrace(new PrintWriter(sw, true));
				String str = sw.toString();
				log.warn(location + str);
			} else {
				log.warn(location + obj.toString());
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	/**
	 * 打印信息
	 * 
	 * @param obj
	 */
	public static void info(Object obj) {
		try {
			/*** 获取输出信息的代码的位置 ***/
			String location = "";
			StackTraceElement[] stacks = Thread.currentThread().getStackTrace();
			location = stacks[2].getClassName() + "."
			        + stacks[2].getMethodName() + "("
			        + stacks[2].getLineNumber() + ")";
			/*** 是否是异常  ***/
			if (obj instanceof Exception) {
				Exception e = (Exception) obj;
				StringWriter sw = new StringWriter();
				e.printStackTrace(new PrintWriter(sw, true));
				String str = sw.toString();
				log.info(location + str);
			} else {
				log.info(location + obj.toString());
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	/**
	 * 打印错误
	 * 
	 * @param obj
	 */
	public static void error(Object obj) {
		try {
			/*** 获取输出信息的代码的位置 ***/
			String location = "";
			StackTraceElement[] stacks = Thread.currentThread().getStackTrace();
			location = stacks[2].getClassName() + "."
			        + stacks[2].getMethodName() + "("
			        + stacks[2].getLineNumber() + ")";

			/*** 是否是异常  ***/
			if (obj instanceof Exception) {
				Exception e = (Exception) obj;
				StringWriter sw = new StringWriter();
				e.printStackTrace(new PrintWriter(sw, true));
				String str = sw.toString();
				log.error(location + str);
			} else {
				log.error(location + obj.toString());
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	/**
	 * 向数据库告警表中插入信息
	 * @param obj
	 */
	public static void dbWarn(Object obj) {
		try {
			String printInfo = "";
			/*** 获取输出信息的代码的位置 ***/
			String location = "";
			StackTraceElement[] stacks = Thread.currentThread().getStackTrace();
			location = stacks[2].getClassName() + "."
			        + stacks[2].getMethodName() + "("
			        + stacks[2].getLineNumber() + ")";

			/*** 是否是异常  ***/
			if (obj instanceof Exception) {
				Exception e = (Exception) obj;
				printInfo = location + e.getMessage();
				log.fatal(printInfo.substring(0,
				        printInfo.length() > 512 ? 512 : printInfo.length()));
			} else {
				printInfo = location + obj.toString();
				log.fatal(printInfo.substring(0,
				        printInfo.length() > 512 ? 512 : printInfo.length()));
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	/**
	 * 获取调用此函数的代码的位置
	 * @return 包名.类名.方法名(行数)
	 */
	public static String getCodeLocation() {
		try {
			/*** 获取输出信息的代码的位置 ***/
			String location = "";
			StackTraceElement[] stacks = Thread.currentThread().getStackTrace();
			location = stacks[2].getClassName() + "."
			        + stacks[2].getMethodName() + "("
			        + stacks[2].getLineNumber() + ")";
			return location;
		} catch (Exception e) {
			// TODO: handle exception
			Log4jUtil.error(e);
			return "";
		}
	}
	public static void main(String[] args) {
		Log4jUtil.info("这是信息");
	}
}
